Fix Pydantic 2.12+ compatibility for custom FieldInfo with Annotated types#727
Merged
Fix Pydantic 2.12+ compatibility for custom FieldInfo with Annotated types#727
Conversation
…types Pydantic 2.12+ converts custom FieldInfo subclasses to plain PydanticFieldInfo for fields using Annotated types with metadata (like Coordinates). This caused custom attributes like index, sortable, etc. to be lost. Fix: Capture original FieldInfo objects before Pydantic processes them and restore them when Pydantic has converted them to plain PydanticFieldInfo.
mypy 1.19+ depends on librt which only has CPython wheels. When Poetry tries to install on PyPy, it falls back to building from source, which fails because librt uses CPython-specific C APIs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pydantic 2.12+ FieldInfo Subclass Regression
The Problem
Pydantic Issue:
#12359—FieldInfo.from_annotationsno longer returns subclasses ofFieldInfo.What Changed in Pydantic 2.12
Pydantic 2.12 introduced a complete refactor of the
FieldInfoclass (PRs#11388and#11898) to fix ~10 separate bugs.This refactor changed the behavior of
FieldInfo.from_annotation():Pydantic 2.11.x and earlier:
When a custom
FieldInfosubclass appeared inAnnotatedmetadata,from_annotation()preserved the subclass type.Pydantic 2.12+:
from_annotation()now returns a plainPydanticFieldInfoinstance instead of preserving the subclass.How This Affected redis-om-python
Redis OM Python uses a custom
FieldInfosubclass with additional Redis-specific attributes:When users define fields with
Annotatedtypes likeCoordinates:Where
Coordinatesis:Under Pydantic 2.12+, the internal steps are:
Annotatedtype and read metadata.FieldInfo.from_annotation().PydanticFieldInfo(not the custom subclass).indexsortableprimary_keyfull_text_searchvector_optionscase_sensitiveThe Fix
The fix preserves the original custom
FieldInfoobjects by capturing them before Pydantic's metaclass runs and restoring them afterward:Will Pydantic Restore the Original Behavior?
No. Per Pydantic issue
#12374:FieldInfoinstances existed in metadata.FieldInfois discouraged — composition is recommended instead:Long-term Implications
The workaround in this PR is appropriate for now.
A more future-proof approach would be refactoring redis-om-python to use composition (custom metadata in
Annotated) rather than inheritance, though this would be a larger change.